<?php
namespace app\sysadmin\controller;
use think\Db;
class Contentfield extends Common{
	private $no_nav_field = array();
	private $no_nav_search = array();
	public function __construct(){
		parent::__construct();
			$this->no_nav_field = array('textarea','img_all','file','basics','simple','complete');
			$this->no_nav_search = array('img','img_all','file','basics','simple','complete');
			$this->have_attr = array('radio','select','checkbox');

			// case 'input': //文本框
			// case 'email': //邮箱
			// case 'mobile': //手机号
			// case 'price': // 金额
			// case 'number': // 数字
			// case 'textarea': // 文本域
			// case 'time': // 时间 H:i:s
			// case 'date': // 日期 Y-m-d
			// case 'datetime': // 日期时间 Y-m-d H:i:s
			// case 'radio': // 单选
			// case 'select': // 下拉
			// case 'checkbox': // 多选
			// case 'img': // 单图
			// case 'img_all': // 多图
			// case 'file': // 附件
			// case 'basics': // 基础
			// case 'simple': // 简单
			// case 'complete': // 完整
	}
	public function index(){
		$_input_data = input('get.');
		$_get_key = array('table_id','cname','field_name','type_group_id','status');
		$_get_str = "";

		if($_input_data){
			foreach($_input_data as $k=>$v){
				$_v = trim($v);
				if(in_array($k,$_get_key)){
					if($_v != ''){
						$_get_str .= $k.":'".strval($_v)."',";
					}else{
						$_input_data[$k] = '';
					}
				}else{
					unset($_input_data[$k]);
				}
			}
		}
		if($_get_key){
			foreach($_get_key as $a){
				if(!isset($_input_data[$a])){
					$_input_data[$a] = '';
				}
			}
		}
		$this->assign('get_data',$_input_data);
		$this->assign('get_str',$_get_str);

		$table_info = $this->get_table_info($_input_data['table_id']);
		if(!$table_info){
			return $this->tips_404();exit;
		}
		$this->assign('type_group',$table_info['type_group']);
		$this->assign('table_info',$table_info['table_info']);

		// print_r(config('app_content.field_type'));
		return $this->fetch();
	}
	public function add(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');

			$_data = array();

			$_data['cname'] = $_post_data['cname'];
			$_data['field_name'] = $_post_data['field_name'];
			$_data['type_group_id'] = (int)$_post_data['type_group_id'];
			$_data['table_id'] = (int)$_post_data['table_id'];
			$_data['type'] = $_post_data['type'];
			$_data['is_required'] = $_post_data['is_required']==1?1:0;
			$_data['is_nav'] = $_post_data['is_nav']==1?1:0;
			$_data['is_search'] = $_post_data['is_search']==1?1:0;
			$_data['status'] = $_post_data['status']==1?1:0;
			$_data['sort'] = (int)$_post_data['sort'];
			$_data['tips'] = $_post_data['tips'];
			$_data['create_time'] = time();
			$_data['update_time'] = time();
			if(in_array($_data['type'],$this->no_nav_field)){
				$_data['is_nav'] = 0;
			}
			if(in_array($_data['type'],$this->no_nav_search)){
				$_data['is_search'] = 0;
			}
			if(!in_array($_data['type'],$this->have_attr)){
				$_data['attr_type'] = '';
			}else{
				$_data['attr_type'] = $_post_data['attr_type'];
			}

			$_ret_id = Db::name('content_model_field')->insertGetId($_data);
			if($_ret_id){
				$this->create_field($_ret_id);
				$this->success("添加成功",url('index'));
			}else{
				$this->error("添加失败");
			}
			exit;
		}
		$table_info = $this->get_table_info(input('get.table_id'));
		if(!$table_info){
			return $this->tips_404();exit;
		}
		// print_r($table_info['field_type']);
		$this->assign('field_type',$table_info['field_type']);
		$this->assign('type_group',$table_info['type_group']);
		$this->assign('table_info',$table_info['table_info']);
		
		$attr = new \Wy\Attr();
		$attr_type = $attr->attr_type();
		$this->assign('attr_type',$attr_type);
		
		$_info = array(
			'id'=>'0',
			'table_id'=>$table_info['table_info']['id'],
			'cname'=>'',
			'field_name'=>'',
			'type_group_id'=>'',
			'type'=>'input',
			'attr_type'=>'',
			'is_required'=>'0',
			'is_nav'=>'0',
			'is_search'=>'0',
			'status'=>'1',
			'sort'=>'0',
			'tips'=>'',
		);
		$this->assign('info',$_info);
		$this->assign('page_name','添加字段');

		// print_r($attr->attr_lists('sex'));
		return $this->fetch('info');
	}
	public function edit(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');

			$_id = (int)$_post_data['id'];
			$_table_id = (int)$_post_data['table_id'];

			$_data = array();
			$_data['cname'] = $_post_data['cname'];
			$_data['type_group_id'] = (int)$_post_data['type_group_id'];
			$_data['is_required'] = $_post_data['is_required']==1?1:0;
			$_data['is_nav'] = $_post_data['is_nav']==1?1:0;
			$_data['is_search'] = $_post_data['is_search']==1?1:0;
			$_data['status'] = $_post_data['status']==1?1:0;
			$_data['sort'] = (int)$_post_data['sort'];
			$_data['tips'] = $_post_data['tips'];
			$_data['update_time'] = time();

			if(in_array($_post_data['type'],$this->no_nav_field)){
				$_data['is_nav'] = 0;
			}
			if(in_array($_post_data['type'],$this->no_nav_search)){
				$_data['is_search'] = 0;
			}
			
			if(!in_array($_post_data['type'],$this->have_attr)){
				$_data['attr_type'] = '';
			}else{
				$_data['attr_type'] = $_post_data['attr_type'];
			}

			$_ret_id = Db::name('content_model_field')->where('id','=',$_id)->where('table_id','=',$_table_id)->update($_data);
			if($_ret_id){
				$this->add_operation_log($_id,$_data);
				$this->success("编辑成功",url('index'));
			}else{
				$this->error("编辑失败");
			}
			exit;
		}
		$_id = (int)(input('get.id'));
		$_info = Db::name('content_model_field')->where('id','=',$_id)->find();
		if(!$_info){
			return $this->tips_404();exit;
		}
		$this->assign('info',$_info);
		$this->assign('page_name','编辑字段');


		$table_info = $this->get_table_info($_info['table_id']);
		if(!$table_info){
			return $this->tips_404();exit;
		}
		$this->assign('field_type',$table_info['field_type']);
		$this->assign('type_group',$table_info['type_group']);
		$this->assign('table_info',$table_info['table_info']);
		$this->assign('field_language',$table_info['field_language']);
		
		$attr = new \Wy\Attr();
		$attr_type = $attr->attr_type();
		$this->assign('attr_type',$attr_type);

		return $this->fetch('info');
	}
	public function del(){
		if($this->request->isGet() == true){
			$_id = (int)input('get.id');
			if(!$_id){
				$this->error("请选择需要删除的信息！");
			}
			$_info = Db::name('content_model_table')->where('id','=',$_id)->where('is_del','=','0')->find();
			if(!$_info){
				$this->error("请选择需要删除的信息！");
			}
			$_ret = Db::name('content_model_table')->where('id','=',$_id)->update(array('is_del',1));
			if($_ret){
				$this->add_operation_log($_id,$_info);
				$this->success("删除成功",url('index'));
			}else{
				$this->error("删除失败，请刷新以后重试！");
			}

		}else{
			$this->error("系统错误！");
		}
	}

	// 异步查询列表
	public function ajax_lists(){
		if($this->request->isAjax() != true){
			$this->error('404');
		}
		$_input_data = input();
		$page = $_input_data['page'] >= 1 ? (int)$_input_data['page'] : '0';
		$limit = $_input_data['limit'] >= 1 ? (int)$_input_data['limit'] : '15';
		unset($_input_data['page']);
		unset($_input_data['limit']);
		$_where = array();
		if(!empty($_input_data['table_id'])){
			$_where[] = array('table_id','eq',$_input_data['table_id']);
		}
		if(!empty($_input_data['field_name'])){
			$_where[] = array('field_name','like','%'.$_input_data['field_name'].'%');
		}
		if(!empty($_input_data['name'])){
			$_where[] = array('cname','like','%'.$_input_data['cname'].'%');
		}

		if(isset($_input_data['type_group_id'])){
			if($_input_data['type_group_id'] != ''){
				$_input_data['type_group_id'] = (int)$_input_data['type_group_id'];
				$_where[] = array('type_group_id','=',$_input_data['type_group_id']);
			}
		}
		if(isset($_input_data['status'])){
			if($_input_data['status'] != ''){
				$_input_data['status'] = (int)$_input_data['status'];
				$_where[] = array('status','=',$_input_data['status']);
			}
		}

		$count = Db::name('content_model_field')->where($_where)->count();
		$lists = Db::name('content_model_field')->where($_where)->page($page.','.$limit)->field('id,table_id,type_group_id,field_name,cname,type,sort,status,create_time,is_nav,is_required,is_search')
					->order('type_group_id asc,sort asc,id asc')->select();
		if($lists){
			$table_info = $this->get_table_info($_input_data['table_id']);
			// 'type_group'=>$type_group,'table_info'=>$_table_info,'field_type'=>$field_type

			foreach($lists as $k=>$v){
				$lists[$k]['create_date'] = date('Y-m-d H:i:s',$v['create_time']);
				$lists[$k]['type_group_name'] = !empty($table_info['type_group'][$v['type_group_id']])?$table_info['type_group'][$v['type_group_id']]:'---';
				$lists[$k]['type_name'] = !empty($table_info['field_language'][$v['type']])?$table_info['field_language'][$v['type']]:'---';
			}
		}
		$_json = array();
		$_json['code'] = 0;
		$_json['msg'] = 'success';
		$_json['count'] = $count;
		$_json['data'] = $lists;
		echo json_encode($_json);
		exit;
	}
	private function get_lists_num($_db_name){
		$_cache_name = 'sysadmin_content_table_count_'.$_db_name;
		$num = cache($_cache_name);
		if($num){
			return $num;
		}
		$num = Db::name($_db_name)->count();
		cache($_cache_name,$num,600);
		return $num;
	}
	private function get_table_info($table_id = 0){
		if(!$table_id){
			return 0;
		}

		$_cache_name = 'sysadmin_get_table_info_'.$table_id;
		$_data_info = cache($_cache_name);
		if($_data_info){
			return $_data_info;
		}

		$_table_info = Db::name('content_model_table')->where('id','=',$table_id)->find();
		if(!$_table_info){
			return 0;
		}
		$type_group = array();
		$_type_group = trim($_table_info['type_group']);
		if($_type_group){
			$type_group = nl2array($_type_group);
			$type_group = array_filter($type_group);
			$type_group = array_values($type_group);
		}else{
			$type_group[] = '基础信息';
		}
		$type_group[99] = 'SEO信息';

		$field_type = config('app_content.field_type');

		$field_language = array();
		if($field_type){
			foreach($field_type as $k=>$v){
				foreach($v['lists'] as $kk=>$vv){
					$field_language[$vv['field']] = $vv['name'];
				}
			}
		}
		$_data_info = array('type_group'=>$type_group,'table_info'=>$_table_info,'field_type'=>$field_type,'field_language'=>$field_language);
		cache($_cache_name,$_data_info,3600);
		return $_data_info;
	}
	private function create_field($_field_id = 0){
		$_field_info = Db::name('content_model_field')->find($_field_id);
		if($_field_info){
			// 添加字段
			$_field_name = $_field_info['field_name'] ? $_field_info['field_name'] : 'fd_'.$_field_info['id'];
			$_tables_name = config('database.prefix').'content_lists_'.$_field_info['table_id'];
			$_sql_add_field = "ALTER TABLE `".$_tables_name."` ADD COLUMN ";

			$_is_error = 0;

			switch ($_field_info['type']){
				case 'input': //文本框
					$_sql_add_field .= " `".$_field_name."` varchar(200) ";
					break;
				case 'email': //邮箱
					$_sql_add_field .= " `".$_field_name."` varchar(100) ";
					break;
				case 'mobile': //手机号
					$_sql_add_field .= " `".$_field_name."` varchar(40) ";
					break;
				case 'price': // 金额
					$_sql_add_field .= " `".$_field_name."` decimal(16,2) DEFAULT 0 ";
					break;
				case 'number': // 数字
					$_sql_add_field .= " `".$_field_name."` varchar(100) ";
					break;
				case 'textarea': // 文本域
					$_sql_add_field .= " `".$_field_name."` varchar(400) ";
					break;
				case 'time': // 时间 H:i:s
					$_sql_add_field .= " `".$_field_name."` time DEFAULT 0 ";

					break;
				case 'date': // 日期 Y-m-d
					$_sql_add_field .= " `".$_field_name."` date DEFAULT 0 ";
					break;
				case 'datetime': // 日期时间 Y-m-d H:i:s
					$_sql_add_field .= " `".$_field_name."` datetime DEFAULT 0 ";
					break;
				case 'radio': // 单选
					$_sql_add_field .= " `".$_field_name."` varchar(40) ";
					break;
				case 'select': // 下拉
					$_sql_add_field .= " `".$_field_name."` varchar(200) ";
					break;
				case 'checkbox': // 多选
					$_sql_add_field .= " `".$_field_name."` varchar(200) ";
					break;
				case 'img': // 单图
					$_sql_add_field .= " `".$_field_name."` varchar(200) ";

					break;
				case 'img_all': // 多图
					$_sql_add_field .= " `".$_field_name."` varchar(400) ";

					break;
				case 'file': // 附件
					$_sql_add_field .= " `".$_field_name."` varchar(200) ";

					break;
				case 'basics': // 基础
					$_sql_add_field .= " `".$_field_name."` MEDIUMTEXT ";

					break;
				case 'simple': // 简单
					$_sql_add_field .= " `".$_field_name."` text ";

					break;
				case 'complete': // 完整
					$_sql_add_field .= " `".$_field_name."` MEDIUMTEXT ";

					break;
				default:
					$_is_error++;
			}
			if($_is_error > 0){
				return false;
			}
			$_sql_add_field .= " NOT NULL COMMENT '".$_field_info['cname']."'";

			Db::query($_sql_add_field);

			// 修改字段名称
			Db::name('content_model_field')->where('id','=',$_field_id)->update(array('field_name'=>$_field_name));
		}
	}
}
?>